package com.yygo.webapp.controller;

import com.yygo.Constants;
import com.yygo.model.BondCompany;
import com.yygo.model.Dealer;
import com.yygo.model.DealerRule;
import com.yygo.model.LabelValue;
import com.yygo.model.Province;
import com.yygo.model.User;
import com.yygo.service.BondManager;
import com.yygo.service.DealerManager;
import com.yygo.service.LoanProductManager;
import com.yygo.service.ProvinceManager;
import com.yygo.service.RoleManager;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

@Controller
@RequestMapping("/helps*")
public class HelpController extends BaseFormController {

    @RequestMapping(method = RequestMethod.GET)
    public static void download(HttpServletRequest request,
                                HttpServletResponse response, String name
    ) throws Exception {
        request.setCharacterEncoding("UTF-8");
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        String ctxPath = request.getSession().getServletContext()
                .getRealPath("");
        String downLoadPath = ctxPath+"/docs/"+ name;
        long fileLength = new File(downLoadPath).length();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment; filename="
                + new String(name.getBytes("utf-8"), "ISO8859-1"));
        response.setHeader("Content-Length", String.valueOf(fileLength));
        bis = new BufferedInputStream(new FileInputStream(downLoadPath));
        bos = new BufferedOutputStream(response.getOutputStream());
        byte[] buff = new byte[2048];
        int bytesRead;
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
            bos.write(buff, 0, bytesRead);
        }
        bis.close();
        bos.close();
    }
}
